package com.b2c.common.utils;

import java.util.Map;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:对象转换工具类 pbd add 2019/4/15 10:48
 */
public class ObjectHelper {
    public static <T> Object convert(Object object, Class<T> type) {
        if ((object == null) || (type == null)) {
            return null;
        }
        if (type.isAssignableFrom(object.getClass())) {
            return object;
        }
        return null;
    }

    public static <T> Object[] convertArray(Object object, Class<T> type) {
        if ((object == null) || (type == null)) {
            return null;
        }
        Class<?> objectClass = object.getClass();
        if ((objectClass.isArray()) && (type == objectClass.getComponentType())) {
            return (Object[]) object;
        }
        return null;
    }

    public static int[] convertIntArray(Object object) {
        if (object == null) {
            return null;
        }
        Class objectClass = object.getClass();
        if ((objectClass.isArray())
                && (Integer.TYPE == objectClass.getComponentType())) {
            return (int[]) object;
        }
        return null;
    }

    public static float[] convertFloatArray(Object object) {
        if (object == null) {
            return null;
        }
        Class objectClass = object.getClass();
        if ((objectClass.isArray())
                && (Float.TYPE == objectClass.getComponentType())) {
            return (float[]) object;
        }
        return null;
    }

    public static double[] convertDoubleArray(Object object) {
        if (object == null) {
            return null;
        }
        Class objectClass = object.getClass();
        if ((objectClass.isArray())
                && (Double.TYPE == objectClass.getComponentType())) {
            return (double[]) object;
        }
        return null;
    }

    public static short[] convertShortArray(Object object) {
        if (object == null) {
            return null;
        }
        Class objectClass = object.getClass();
        if ((objectClass.isArray())
                && (Short.TYPE == objectClass.getComponentType())) {
            return (short[]) object;
        }
        return null;
    }

    public static long[] convertLongArray(Object object) {
        if (object == null) {
            return null;
        }
        Class objectClass = object.getClass();
        if ((objectClass.isArray())
                && (Long.TYPE == objectClass.getComponentType())) {
            return (long[]) object;
        }
        return null;
    }

    public static boolean[] convertBooleanArray(Object object) {
        if (object == null) {
            return null;
        }
        Class objectClass = object.getClass();
        if ((objectClass.isArray())
                && (Boolean.TYPE == objectClass.getComponentType())) {
            return (boolean[]) object;
        }
        return null;
    }

    public static byte[] convertByteArray(Object object) {
        if (object == null) {
            return null;
        }
        Class objectClass = object.getClass();
        if ((objectClass.isArray())
                && (Byte.TYPE == objectClass.getComponentType())) {
            return (byte[]) object;
        }
        return null;
    }

    /**
     * 将Object对象里面的属性和值转化成Map对象
     *
     * @param obj
     * @return
     * @throws IllegalAccessException
     */
    public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        Class<?> clazz = obj.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            String fieldName = field.getName();
            Object value = field.get(obj);
            map.put(fieldName, value);
        }
        return map;
    }

}
